import vue from 'vue'
import Vuex from 'vuex'
import router from '../router'
vue.use(Vuex)

function addNewRoute(menuList){
    // menuList 数据库传过来的
    console.log(menuList);
    
    let routes = router.options.routes
    console.log(routes);
    routes.forEach(routeItem=>{
        if(routeItem.path == '/Index'){
            menuList.forEach(menu=>{
                let childRoute = {
                        path:'/'+menu.menuclick,
                        name:menu.menuname,
                        meta:{
                            title:menu.menuname
                        },
                        component:()=>import('../components/'+menu.menucomponent)
                    }
                    routeItem.children.push(childRoute)
            })
        }
    })
    router.addRoutes(routes)
    
    // {
    //     path:'/Drug',
    //     name:'drug',
    //     meta:{
    //         title:'药品管理'
    //     },
    //     component:()=>import('../components/Drug/DrugManger')
    // }

}

export default new Vuex.Store({
    state:{
        menu:[]
    },
    mutations:{
        setMenu(state,menuList){
            state.menu = menuList

            addNewRoute(menuList)
        }
    },
    getters:{
        getMenu(state){
            return state.menu
        }
    }
})